﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

public class UIStart : MonoBehaviour {

    private static UIStart _instance;
    public static UIStart Instance { get { return _instance; } }

    private void Awake()
    {
        _instance = this;
        InitWidget();
        AddListeners();
        HidePanel();
    }

    private Button _startBtn;
    private Button _settingBtn;
    private Button _backBtn;
    private GameObject _hintPanel;

    private Image[] _childImgs;

    private void InitWidget()
    {
        _startBtn = transform.Find("StartBtn").GetComponent<Button>();
        _settingBtn = transform.Find("SettingBtn").GetComponent<Button>();
        _childImgs = transform.GetComponentsInChildren<Image>();
        _hintPanel = transform.Find("HintPanel").gameObject;
        _backBtn = _hintPanel.transform.Find("BackBtn").GetComponent<Button>();
    }

    private void AddListeners()
    {
        _startBtn.onClick.AddListener(StartGame);
        _settingBtn.onClick.AddListener(ShowPanel);
        _backBtn.onClick.AddListener(HidePanel);
    }

    private void StartGame()
    {
        GameManager.Instance.StartGame();
        //渐隐消失.
        //gameObject.SetActive(false);
        StartCoroutine(FadePanel());
    }

    private IEnumerator FadePanel()
    {
        for (int i = 0; i < _childImgs.Length; i++)
        {
            _childImgs[i].DOFade(0, 0.5f);
        }
        yield return new WaitForSeconds(0.5f);
        gameObject.SetActive(false);
    }

    private void HidePanel()
    {
        _hintPanel.SetActive(false);
    }

    private void ShowPanel()
    {
        _hintPanel.SetActive(true);
    }
}
